package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Category;
import com.mall.db.domain.Keyword;
import com.mall.db.domain.KeywordAndCategory;
import com.mall.db.service.CategoryService;
import com.mall.db.service.KeywordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/system/category")
public class CategoryController extends BaseController<Category>{
    @Resource(name = "categoryService")
    private CategoryService cs;

    @Resource(name = "keywordService")
    private KeywordService ks;

    @Override
    public Map<String, Object> find(Category category) {
        PageHelper.startPage(category.getPage(),category.getLimit());

        QueryWrapper<Category> qw = new QueryWrapper<Category>();

        List<Category> list = cs.list(qw);
        for (int i = 0; i < list.size() ; i++){
            Keyword keyword = ks.getById(list.get(0).getKid());
            list.get(i).setKeyword(keyword.getKeyword());
        }

        PageInfo<Category> page = new PageInfo<>(list,category.getLimit());
        return findMap(list,page.getTotal());
    }

    @RequestMapping("/findAll")
    public Map<String,Object> findAll(Category category){

        QueryWrapper<Category> qw = new QueryWrapper<>();

        List<Category> list = cs.list(qw);

        return this.findMap(list,list.size());
    }
    
    @RequestMapping("/findCK")
    public Map<String,Object> findCK(Category category){

        QueryWrapper<Category> qw = new QueryWrapper<>();

        List<Category> list = cs.list(qw);
        for (int i = 0; i < list.size() ; i++){
            Keyword keyword = ks.getById(list.get(0).getKid());
            list.get(i).setKeyword(keyword.getKeyword());
        }

        return this.findMap(list,list.size());
    }



    @RequestMapping("/findCAK")
    public Map<String,Object> findCategoryAndKeyword(Category category){

        QueryWrapper<Category> cqw = new QueryWrapper<>();
        QueryWrapper<Keyword> kqw = new QueryWrapper<>();
        List<Category> Clist = cs.list(cqw);
        List<Keyword> Klist = ks.list(kqw);

        List<KeywordAndCategory> ckList = new ArrayList<>();
        for (Category c:Clist) {
            KeywordAndCategory kac = new KeywordAndCategory(c.getId(),c.getName());
            ckList.add(kac);
        }
        for (Keyword k:Klist) {
            KeywordAndCategory kac = new KeywordAndCategory(k.getId(),k.getKeyword());
            ckList.add(kac);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",ckList);
        map.put("count",ckList.size());
        //日志
        map.put("is",true);
        map.put("msg","查询成功");
        return map;
    }




    @Override
    public Map<String, Object> add(Category category) {
        category.setCreateTime((new Date()));
        boolean b = cs.save(category);
        return iud("新增",b,category);
    }

    @Override
    public Map<String, Object> update(Category category) {
        boolean b = cs.updateById(category);
        return iud("修改",b,category);
    }

    @Override
    public Map<String, Object> delete(Category category) {
        boolean b = cs.removeById(category);
        return iud("删除",b,category);
    }
}
